package chapter6;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * Created by uu on 2017/6/23.
 */
public class load_balancing_customer {


    public static void main(String[] args) throws  Exception{
        ConnectionFactory factory =  new ConnectionFactory();
        factory.setUsername("alert_user");
        factory.setPassword("alertme");
        factory.setVirtualHost("/");
        factory.setHost("localhost");
        factory.setPort(5670);
        while(true){
            Connection conn = factory.newConnection();
            final Channel channel = conn.createChannel();
            channel.exchangeDeclare("balance-exchange","direct",true,false,null);
            channel.queueDeclare("balance-queue",true,false,false,null);
            channel.queueBind("balance-queue","balance-exchange","balance",null);
            System.out.println("Ready for testing");
            channel.basicConsume("balance-queue",false, "balance-tag",
                    new DefaultConsumer(channel) {
                        @Override
                        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                            String routingKey = envelope.getRoutingKey();
                            String contentType = properties.getContentType();
                            long deliveryTag = envelope.getDeliveryTag();
                            // (process the message components here ...)
                            System.out.println(new String(body, "UTF-8"));
                            channel.basicAck(deliveryTag, false);
                        }
                    });

        }
    }
}
